package net.bbtang.website.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.codec.binary.Base64;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyun.oss.OSSClient;


import net.bbtang.website.pojo.UserPublishInfoDTO;
import net.bbtang.website.utils.ConstantsStatus;
import net.bbtang.website.utils.CustomizedPropertyPlaceholderConfigurer;
import net.bbtang.website.utils.FileUtils;
import net.bbtang.website.utils.OSSConfigure;
import net.bbtang.website.utils.StatusCode;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/update")
public class UpdateController {
	
	private static OSSConfigure  configure = new OSSConfigure();
	static {
//		configure.setAccessKeyId(CustomizedPropertyPlaceholderConfigurer.getContextProperty("")+"");
//		configure.setAccessKeySecret(CustomizedPropertyPlaceholderConfigurer.getContextProperty("")+"");
//		configure.setBucketName(CustomizedPropertyPlaceholderConfigurer.getContextProperty("")+"");
//		configure.setEndpoint(CustomizedPropertyPlaceholderConfigurer.getContextProperty("")+"");
		configure.setAccessKeyId("LTAIbZYdKAYfbZVr");
		configure.setAccessKeySecret("cJ31UrQfH97Sq9Yvxr6B1kJNNF8yLI");
		configure.setBucketName("btang");
		configure.setEndpoint("http://oss-cn-shanghai.aliyuncs.com");
	}
	
	
	
	/**发布发布接口
	 @api {post} /update/updateImage 发布发布接口
	 
	 @apiParam {String} file base64文件字节编码
	 @apiParam {String} fileName 文件名字，会覆盖原有文件
	 
	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"file":10
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 200 OK
	{
	    "flag":false,
	    "number":"详细错误信息",
	    "detailMsg":"错误错误".
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author zx
	 */
	@RequestMapping(value = "/updateImage", method = RequestMethod.POST)
	@ResponseBody
	public Object updateImage(@RequestBody Object body){
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			String fileStr =jb.getString("file");
			String fileName = jb.getString("fileName");
			FileUtils.uploadfile2oss(configure,fileName,base64ToInputStream(fileStr));
		} catch (Exception e) {
			e.printStackTrace();
			sc.setFlag(false);
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("上传图片异常.");
		}
		
		return sc;
	}
	
	private InputStream base64ToInputStream(String base64) {
		InputStream in = new ByteArrayInputStream(Base64.decodeBase64(base64));  
		return in;
    }
	
	
	public static void main (String[] args){
		
		byte[] buffer = null;  
        try {  
        	File f = new File("D:/1.jpg");  
            FileInputStream fis = new FileInputStream(f);  
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
            byte[] b = new byte[(int)f.length()];  
            int n;  
            while ((n = fis.read(b)) != -1) {  
                bos.write(b, 0, n);  
            }  
            fis.close();  
            bos.close();  
            buffer = bos.toByteArray();  
            String s  = Base64.encodeBase64String(buffer);
            System.out.println(s);
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
         
		
		
		
		
		
	}
}
